package com.zxc.reggie.service.lmpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.reggie.common.CustomException;
import com.zxc.reggie.entity.Category;
import com.zxc.reggie.entity.Dish;
import com.zxc.reggie.entity.Setmeal;
import com.zxc.reggie.mapper.CategoryMapper;
import com.zxc.reggie.service.CategoryService;
import com.zxc.reggie.service.DishService;
import com.zxc.reggie.service.SetMealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private SetMealService setMealService;
    @Resource
    private DishService dishService;

    @Override
    public void delete(Long id) {


        LambdaQueryWrapper<Setmeal> SetMealQueryWrapper = new LambdaQueryWrapper<>();
        SetMealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int SetMealCount = setMealService.count(SetMealQueryWrapper);

        if (SetMealCount > 0) {
            throw new CustomException("当前分类下关联了其他菜单，不能删除");
        }

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int DishCount = dishService.count(dishLambdaQueryWrapper);

        if (DishCount > 0) {
            throw new CustomException("当前分类下关联了其他菜品，不能删除");
        }

        removeById(id);
    }


}
